package com.dime.physical.service.impl;

import com.dime.basic.api.RemoteBaseMaincheckHarmAdvice;
import com.dime.basic.api.RemoteBaseMainclusionService;
import com.dime.basic.api.RemoteHealthadviceService;
import com.dime.basic.api.RemoteOccupationcriterionService;
import com.dime.basic.api.domain.*;
import com.dime.basic.api.model.healthDto;
import com.dime.mninterface.api.RemoteHealth100Service;
import com.dime.mninterface.api.model.MNResponseBase;
import com.dime.mninterface.api.model.PackageItem;
import com.dime.mninterface.api.model.RecheckRequestBody;
import com.dime.physical.domain.*;
import com.dime.physical.domain.dto.*;
import com.dime.physical.mapper.*;
import com.dime.physical.service.IDimeMainCheckService;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.uuid.IdUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.UserShop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class DimeMainCheckServiceImpl implements IDimeMainCheckService {

    @Autowired
    private DimePhysicalregisterMapper dimePhysicalregisterMapper;


    @Autowired
    private DimePhysicalharmsMapper dimePhysicalharmsMapper;


    @Autowired
    private DimePhysicalitemsMapper dimePhysicalitemsMapper;


    @Autowired
    private DimePhysicalconsionMapper dimePhysicalconsionMapper;


    @Autowired
    private RemoteOccupationcriterionService remoteOccupationcriterionService;


    @Autowired
    private RemoteHealthadviceService remoteHealthadviceService;

    @Autowired
    private RemoteHealth100Service remoteHealth100Service;


    @Autowired
    private DimeMaincheckconclusionMapper dimeMaincheckconclusionMapper;


    @Autowired
    private DimeMaincheckharmconclusionMapper dimeMaincheckharmconclusionMapper;

    @Autowired
    private DimeMaincheckharmadviceMapper dimeMaincheckharmadviceMapper;


    @Autowired
    private DimeMaincheckhealthadviceMapper dimeMaincheckhealthadviceMapper;


    @Autowired
    private DimeMaincheckharmabnormalcombMapper dimeMaincheckharmabnormalcombMapper;

    @Autowired
    private DimeMaincheckharmrecheckcombMapper dimeMaincheckharmrecheckcombMapper;


    @Autowired
    private DimeMaincheckharmoccupationaldiseaeMapper dimeMaincheckharmoccupationaldiseaeMapper;

    @Autowired
    private DimeMaincheckharmcontraindicationMapper dimeMaincheckharmcontraindicationMapper;

    @Autowired
    private DimeMaincheckharmotherdiseaseMapper dimeMaincheckharmotherdiseaseMapper;

    @Autowired
    private DimeBasecompanyMapper dimeBasecompanyMapper;


    @Autowired
    private RemoteBaseMaincheckHarmAdvice remoteBaseMaincheckHarmAdvice;


    @Autowired
    private DimeMaincheckotherdiseaseMapper dimeMaincheckotherdiseaseMapper;

    @Autowired
    private DimeMaincheckotherdiseaecombMapper dimeMaincheckotherdiseaecombMapper;

    @Autowired
    private RemoteBaseMainclusionService remoteBaseMainclusionService;

    @Autowired
    private DimePhysicalpictureMapper dimePhysicalpictureMapper;

    @Autowired
    private DimePhysicalregisterServiceImpl dimePhysicalregisterService;

    @Autowired
    private DimeInquiryServiceImpl dimeInquiryService;

    @Autowired
    private DimePhysicaladviceMapper dimePhysicaladviceMapper;

    @Autowired
    private DimeBasepackageMapper dimeBasepackageMapper;



    @Override
    public MaincheckViewDto getMaincheckPhysical(DimePhysicalregister searchPhysicalregister) {

        MaincheckViewDto physicalregister = new MaincheckViewDto();

        DimePhysicalregister _physicalregister = dimePhysicalregisterMapper.selectDimePhysicalregisterById(searchPhysicalregister.getId());

        if (_physicalregister != null) {

            physicalregister = MappingPhysical(_physicalregister);

            //人员危害
            DimePhysicalharms searchPhysicalharms = new DimePhysicalharms();
            searchPhysicalharms.setPhysicalRegisterId(searchPhysicalregister.getId());
            List<DimePhysicalharms> physicalharms = dimePhysicalharmsMapper.selectDimePhysicalharmsList(searchPhysicalharms);


            //项目
            DimePhysicalitems searchPhysicalitems = new DimePhysicalitems();
            searchPhysicalitems.setPhysicalRegisterId(searchPhysicalregister.getId());
            List<DimePhysicalitems> physicalitems = dimePhysicalitemsMapper.selectDimePhysicalitemsList(searchPhysicalitems);


            if (physicalitems == null || physicalitems.size() == 0) {
                throw new ServiceException(searchPhysicalregister.getName() + "未查询到人员项目信息！");
            }

            physicalregister.setDimePhysicalitemsList(physicalitems.stream().sorted(Comparator.comparing(DimePhysicalitems::getCombName)).collect(Collectors.toList()));

            //异常项目
            List<DimePhysicalitems> abnormalphysicalitems = physicalitems.stream().filter(x -> x.getPassTag() == 0).collect(Collectors.toList());


            if (abnormalphysicalitems.size() > 0) {

                physicalregister.allabnormalitemsList = new ArrayList<>();

                for (DimePhysicalitems dimePhysicalitems : abnormalphysicalitems) {

                    if (physicalregister.allabnormalitemsList.stream().noneMatch(x -> x.getCombCode().contains(dimePhysicalitems.getCombCode()))) {

                        MaincheckAllabnormalitemsDto maincheckAllabnormalitemsDto = new MaincheckAllabnormalitemsDto();
                        maincheckAllabnormalitemsDto.setCombCode(dimePhysicalitems.getCombCode());
                        maincheckAllabnormalitemsDto.setPassTag(dimePhysicalitems.getPassTag());
                        maincheckAllabnormalitemsDto.setCombName(dimePhysicalitems.getCombName());
                        maincheckAllabnormalitemsDto.setBaseCombId(dimePhysicalitems.getBaseCombId());
                        maincheckAllabnormalitemsDto.setGroupName(dimePhysicalitems.getGroupName());
                        maincheckAllabnormalitemsDto.setBaseGroupId(dimePhysicalitems.getBaseGroupId());


                        DimePhysicalconsion dimePhysicalconsion = dimePhysicalconsionMapper.selectDimePhysicalconsionByPhysicalIdAndGroupId(dimePhysicalitems.getPhysicalRegisterId(), dimePhysicalitems.getBaseGroupId());
                        if (dimePhysicalconsion != null) {
                            maincheckAllabnormalitemsDto.setGroupconsion(dimePhysicalconsion.getConclusion());
                        }
                        physicalregister.allabnormalitemsList.add(maincheckAllabnormalitemsDto);
                    }

                }
            }


            //分析危害
            if (physicalharms != null && physicalharms.size() > 0) {

                physicalregister.maincheckHarmViewDtoList = new ArrayList<>();

                for (DimePhysicalharms dimePhysicalharm : physicalharms) {

                    MaincheckHarmViewDto maincheckHarmDto = getMaincheckHarmViewDto(dimePhysicalharm, abnormalphysicalitems, searchPhysicalregister);
                    if (maincheckHarmDto != null) {
                        physicalregister.maincheckHarmViewDtoList.add(maincheckHarmDto);
                    }

                }
            }

            List<String> groupids = abnormalphysicalitems.stream().map(DimePhysicalitems::getBaseGroupId).distinct().collect(Collectors.toList());

            if (groupids.size() > 0) {

                //基础小组的健康建议
                String[] groupidsArray = groupids.toArray(new String[0]);
                R<List<healthDto>> RhealthDtoList = remoteHealthadviceService.gethealthDtoList(groupidsArray, SecurityConstants.INNER);
                if (RhealthDtoList.getCode() != R.FAIL) {
                    List<healthDto> healthDtos = RhealthDtoList.getData();
                    if (healthDtos != null && healthDtos.size() > 0) {
                        physicalregister.setBasehealthDtoList(healthDtos);
                    }

                    //默认健康建议
                    DimePhysicalconsion searchconclusion = new DimePhysicalconsion();
                    searchconclusion.setPhysicalRegisterId(physicalregister.getId());

                    List<DimePhysicalconsion> dimePhysicalconsionList = dimePhysicalconsionMapper.selectDimePhysicalconsionList(searchconclusion);


                    List<DimeBasehealthadvice> dimeBasehealthadviceList = null;
                    if (healthDtos != null) {
                        dimeBasehealthadviceList = healthDtos.stream().map(healthDto::getDimeBasehealthadviceList).collect(Collectors.toList())
                                .stream().flatMap(Collection::stream).collect(Collectors.toList());
                    }

                    if (dimePhysicalconsionList.size() > 0 && dimeBasehealthadviceList != null) {
                        List<DimeBasehealthadvice> Maincheckhealthadvice = new ArrayList<>();
                        physicalregister.maincheckhealthadviceList = new ArrayList<>();

                        for (DimeBasehealthadvice _healdto : dimeBasehealthadviceList) {

                            try {
                                if (dimePhysicalconsionList.stream().anyMatch(x -> x.getConclusion().contains(_healdto.getDiseasename()))) {
                                    Maincheckhealthadvice.add(_healdto);
                                }
                            } catch (Exception exception) {
                                continue;
                            }


                        }
                        physicalregister.setMaincheckhealthadviceList(Maincheckhealthadvice);
                    }

                }
            }


            //分析 异常结果和  健康建议
            StringBuilder stringBuilder = new StringBuilder();
            physicalregister.abnormalResults = "";

            List<DimePhysicalconsion> dimePhysicalconsionList = dimePhysicalconsionMapper.selectDimePhysicalconsionByPhysicalId(physicalregister.getId());

            if (dimePhysicalconsionList != null && dimePhysicalconsionList.size() > 0) {
                for (DimePhysicalconsion dimePhysicalconsion : dimePhysicalconsionList) {
                    if (!Objects.equals(dimePhysicalconsion.getPassTag(), "1")) {
                        stringBuilder.append(dimePhysicalconsion.getConclusion()).append(";\n");
                    }

                }
            }

            physicalregister.abnormalResults = stringBuilder.toString();


            //人员图片


            List<PhysicalGroupPicDto> physicalGroupPicDtoList = getPhysicalGroupPic(physicalregister);

            physicalregister.setPhysicalGroupPicDtoList(physicalGroupPicDtoList);


            //扁鹊检验
            List<DimePhysicaladvice>dimePhysicaladviceList=dimePhysicaladviceMapper.selectDimePhysicaladviceByPhysicalId(physicalregister.getId());
            physicalregister.setDimePhysicaladviceList(dimePhysicaladviceList);



            //问诊
            InquiryDto inquiryDtos = dimeInquiryService.inquiryDto(physicalregister.getCheckCode());

            physicalregister.setInquiryDto(inquiryDtos);

        }
        return physicalregister;
    }






    private List<PhysicalGroupPicDto>  getPhysicalGroupPic( DimePhysicalregister dimePhysicalregister)
    {

        List<DimePhysicalpicture>dimePhysicalpictureList=dimePhysicalpictureMapper.selectDimePhysicalpictureByphysicalRegisterId(dimePhysicalregister.getId());

        List<PhysicalGroupPicDto> physicalGroupPicDtoList=new ArrayList<>();

        if(dimePhysicalpictureList!=null&&dimePhysicalpictureList.size()>0)
        {
            List<String>groups=dimePhysicalpictureList.stream().map(DimePhysicalpicture::getDimeBasegroupid).distinct().collect(Collectors.toList());
            if(groups.size()>0)
            {

                for (String group:groups) {

                    PhysicalGroupPicDto physicalGroupPicDto=new PhysicalGroupPicDto();

                    List<DimePhysicalpicture>dimePhysicalGorouppictures=dimePhysicalpictureList.stream().filter(x-> Objects.equals(x.getDimeBasegroupid(), group)).collect(Collectors.toList());

                    List<String>urls=new ArrayList<>();
                    for (DimePhysicalpicture dimePhysicalpicture:dimePhysicalGorouppictures) {

                        physicalGroupPicDto.setPhysicalRegisterId(dimePhysicalregister.getId());
                        physicalGroupPicDto.setGroupName(dimePhysicalpicture.getGroupName());
                        physicalGroupPicDto.setDimeBasegroupid(dimePhysicalpicture.getDimeBasegroupid());
                        urls.add(dimePhysicalpicture.getFilepath());
                        // physicalGroupPicDto.setPicUrl().add(dimePhysicalpicture.getFilepath());
                    }
                    physicalGroupPicDto.setPicUrl(urls);
                    physicalGroupPicDtoList.add(physicalGroupPicDto);

                }

            }
        }

        return physicalGroupPicDtoList;
    }


    private MaincheckViewDto MappingPhysical(DimePhysicalregister physicalregister) {
        MaincheckViewDto maincheckViewDto = new MaincheckViewDto();
        maincheckViewDto.setId(physicalregister.getId());
        maincheckViewDto.setAge(physicalregister.getAge());
        maincheckViewDto.setAddress(physicalregister.getAddress());
        maincheckViewDto.setBirthday(physicalregister.getBirthday());
        maincheckViewDto.setBaseCompanyId(physicalregister.getBaseCompanyId());
        maincheckViewDto.setBasePackageId(physicalregister.getBasePackageId());
        maincheckViewDto.setChannel(physicalregister.getChannel());
        maincheckViewDto.setCheckCode(physicalregister.getCheckCode());
        maincheckViewDto.setCheckType(physicalregister.getCheckType());
        maincheckViewDto.setCheckDate(physicalregister.getCheckDate());
        maincheckViewDto.setCompanyName(physicalregister.getCompanyName());
        maincheckViewDto.setCompletStatus(physicalregister.getCompletStatus());
        maincheckViewDto.setEmpNo(physicalregister.getEmpNo());
        maincheckViewDto.setIdNo(physicalregister.getIdNo());
        maincheckViewDto.setIdNoType(physicalregister.getIdNoType());
        maincheckViewDto.setJobType(physicalregister.getJobType());
        maincheckViewDto.setDept(physicalregister.getDept());
        maincheckViewDto.setLastCheckCode(physicalregister.getLastCheckCode());
        maincheckViewDto.setMainConclusion(physicalregister.getMainConclusion());
        maincheckViewDto.setPhone(physicalregister.getPhone());
        maincheckViewDto.setHarmfacter(physicalregister.getHarmfacter());
        maincheckViewDto.setName(physicalregister.getName());
        maincheckViewDto.setCombLack(physicalregister.getCombLack());
        maincheckViewDto.setMarriage(physicalregister.getMarriage());
        maincheckViewDto.setPersonType(physicalregister.getPersonType());
        maincheckViewDto.setWorkYears(physicalregister.getWorkYears());
        maincheckViewDto.setWorkStatus(physicalregister.getWorkStatus());
        maincheckViewDto.setWorkMonths(physicalregister.getWorkMonths());
        maincheckViewDto.setTouchYears(physicalregister.getTouchYears());
        maincheckViewDto.setTouchMonths(physicalregister.getTouchMonths());
        maincheckViewDto.setTouchHarms(physicalregister.getTouchHarms());
        maincheckViewDto.setSocialCode(physicalregister.getSocialCode());
        maincheckViewDto.setShopCode(physicalregister.getShopCode());
        maincheckViewDto.setSex(physicalregister.getSex());
        maincheckViewDto.setSoaOrderNo(physicalregister.getSoaOrderNo());
        maincheckViewDto.setServiceUid(physicalregister.getServiceUid());
        maincheckViewDto.setRegisterDate(physicalregister.getRegisterDate());
        maincheckViewDto.setReCheckTag(physicalregister.getReCheckTag());
        maincheckViewDto.setPackageName(physicalregister.getPackageName());
        maincheckViewDto.setPersonType(physicalregister.getPersonType());

        maincheckViewDto.setStartworkDate(physicalregister.getStartworkDate());
        maincheckViewDto.setTouharmDate(physicalregister.getTouharmDate());

        maincheckViewDto.setMainCheckRevoke(physicalregister.getMainCheckRevoke());
        maincheckViewDto.setMainCheckRevokeMark(physicalregister.getMainCheckRevokeMark());
        return maincheckViewDto;

    }


    private MainchecksubmitDto submitMappingPhysical(DimePhysicalregister physicalregister) {
        MainchecksubmitDto mainchecksubmitDto = new MainchecksubmitDto();
       mainchecksubmitDto.setId(physicalregister.getId());
       mainchecksubmitDto.setAge(physicalregister.getAge());
       mainchecksubmitDto.setAddress(physicalregister.getAddress());
       mainchecksubmitDto.setBirthday(physicalregister.getBirthday());
       mainchecksubmitDto.setBaseCompanyId(physicalregister.getBaseCompanyId());
       mainchecksubmitDto.setBasePackageId(physicalregister.getBasePackageId());
       mainchecksubmitDto.setChannel(physicalregister.getChannel());
       mainchecksubmitDto.setCheckCode(physicalregister.getCheckCode());
       mainchecksubmitDto.setCheckType(physicalregister.getCheckType());
       mainchecksubmitDto.setCheckDate(physicalregister.getCheckDate());
       mainchecksubmitDto.setCompanyName(physicalregister.getCompanyName());
       mainchecksubmitDto.setCompletStatus(physicalregister.getCompletStatus());
       mainchecksubmitDto.setEmpNo(physicalregister.getEmpNo());
       mainchecksubmitDto.setIdNo(physicalregister.getIdNo());
       mainchecksubmitDto.setIdNoType(physicalregister.getIdNoType());
       mainchecksubmitDto.setJobType(physicalregister.getJobType());
       mainchecksubmitDto.setDept(physicalregister.getDept());
       mainchecksubmitDto.setLastCheckCode(physicalregister.getLastCheckCode());
       mainchecksubmitDto.setMainConclusion(physicalregister.getMainConclusion());
       mainchecksubmitDto.setPhone(physicalregister.getPhone());
       mainchecksubmitDto.setHarmfacter(physicalregister.getHarmfacter());
       mainchecksubmitDto.setName(physicalregister.getName());
       mainchecksubmitDto.setCombLack(physicalregister.getCombLack());
       mainchecksubmitDto.setMarriage(physicalregister.getMarriage());
       mainchecksubmitDto.setPersonType(physicalregister.getPersonType());
       mainchecksubmitDto.setWorkYears(physicalregister.getWorkYears());
       mainchecksubmitDto.setWorkStatus(physicalregister.getWorkStatus());
       mainchecksubmitDto.setWorkMonths(physicalregister.getWorkMonths());
       mainchecksubmitDto.setTouchYears(physicalregister.getTouchYears());
       mainchecksubmitDto.setTouchMonths(physicalregister.getTouchMonths());
       mainchecksubmitDto.setTouchHarms(physicalregister.getTouchHarms());
       mainchecksubmitDto.setSocialCode(physicalregister.getSocialCode());
       mainchecksubmitDto.setShopCode(physicalregister.getShopCode());
       mainchecksubmitDto.setSex(physicalregister.getSex());
       mainchecksubmitDto.setSoaOrderNo(physicalregister.getSoaOrderNo());
       mainchecksubmitDto.setServiceUid(physicalregister.getServiceUid());
       mainchecksubmitDto.setRegisterDate(physicalregister.getRegisterDate());
       mainchecksubmitDto.setReCheckTag(physicalregister.getReCheckTag());
       mainchecksubmitDto.setPackageName(physicalregister.getPackageName());
       mainchecksubmitDto.setPersonType(physicalregister.getPersonType());

        mainchecksubmitDto.setStartworkDate(physicalregister.getStartworkDate());
        mainchecksubmitDto.setTouharmDate(physicalregister.getTouharmDate());

        mainchecksubmitDto.setMainCheckRevoke(physicalregister.getMainCheckRevoke());
        mainchecksubmitDto.setMainCheckRevokeMark(physicalregister.getMainCheckRevokeMark());
        return mainchecksubmitDto;

    }


    public   MaincheckHarmViewDto getMaincheckHarmViewDto(DimePhysicalharms dimePhysicalharm,List<DimePhysicalitems> abnormalphysicalitems,DimePhysicalregister searchPhysicalregister) {

        MaincheckHarmViewDto maincheckHarmDto = new MaincheckHarmViewDto();
        maincheckHarmDto.setId(dimePhysicalharm.getBaseHarmId());
        maincheckHarmDto.setPhysicalRegisterId(dimePhysicalharm.getPhysicalRegisterId());
        maincheckHarmDto.setBaseHarmId(dimePhysicalharm.getBaseHarmId());
        maincheckHarmDto.setBaseHarmName(dimePhysicalharm.getBaseHarmName());

        DimeBaseoccupationcriterion search = new DimeBaseoccupationcriterion();

        search.setWorkStatus(searchPhysicalregister.getWorkStatus());
        search.setHarmId(dimePhysicalharm.getBaseHarmId());

        R<List<DimeBaseoccupationcriterion>> listR = remoteOccupationcriterionService.getDimeBaseoccupationcriterionList(search, SecurityConstants.INNER);
        if (listR.getCode() != R.FAIL) {
            List<DimeBaseoccupationcriterion> dimeBaseoccupationcriterionList = listR.getData();

            if (dimeBaseoccupationcriterionList == null)
                return maincheckHarmDto;

            DimeBaseoccupationcriterion dimeBaseoccupationcriterion = dimeBaseoccupationcriterionList.stream().findFirst().orElse(null);

            if (dimeBaseoccupationcriterion != null) {

                //危害对应必检项目
                if (dimeBaseoccupationcriterion.getDimeBasecombcriterionList() != null && abnormalphysicalitems != null && dimeBaseoccupationcriterion.getDimeBasecombcriterionList().size() > 0) {

                    maincheckHarmDto.dimeBasecombcriterionDtoList = new ArrayList<>();
                    for (DimeBasecombcriterion dimeBasecombcriterion : dimeBaseoccupationcriterion.getDimeBasecombcriterionList()) {
                        if (dimeBasecombcriterion != null && abnormalphysicalitems.stream().anyMatch(x -> x.getBaseCombId().equals(dimeBasecombcriterion.getCombId()))) {
                            if (dimeBasecombcriterion.getRequired() != null && dimeBasecombcriterion.getRequired().equals("1")) {


                                DimeBasecombcriterionDto basecombcriterionDto = Mappingcriterion(dimeBasecombcriterion);

                                DimePhysicalitems dimePhysicalitems = abnormalphysicalitems.stream().filter(x -> Objects.equals(x.getBaseCombId(), dimeBasecombcriterion.getCombId())).findAny().orElse(null);

                                if (dimePhysicalitems != null) {


                                    DimePhysicalconsion dimePhysicalconsion = dimePhysicalconsionMapper.selectDimePhysicalconsionByPhysicalIdAndGroupId(searchPhysicalregister.getId(), dimePhysicalitems.getBaseGroupId());
                                    if (dimePhysicalconsion != null) {
                                        basecombcriterionDto.setGroupId(dimePhysicalconsion.getDimeBasegroupid());
                                        basecombcriterionDto.setGroupName(dimePhysicalitems.getGroupName());
                                        basecombcriterionDto.setGroupConsion(dimePhysicalconsion.getConclusion());
                                    }

                                }
                                maincheckHarmDto.dimeBasecombcriterionDtoList.add(basecombcriterionDto);
                            }
                        }
                    }
                }
                maincheckHarmDto.setDimeBaseoccupationaldiseasecriterionList(dimeBaseoccupationcriterion.getDimeBaseoccupationcriterionList());
                maincheckHarmDto.setDimeBasecontraindicationcriterionList(dimeBaseoccupationcriterion.getDimeBasecontraindicationcriterionList());
            }
        }

        return maincheckHarmDto;
    }

    private  DimeBasecombcriterionDto Mappingcriterion(DimeBasecombcriterion dimeBasecombcriterion)
    {
        DimeBasecombcriterionDto dimeBasecombcriterionDto = new DimeBasecombcriterionDto();
        dimeBasecombcriterionDto.setId(dimeBasecombcriterion.getId());
        dimeBasecombcriterionDto.setCriterionId(dimeBasecombcriterion.getCriterionId());
        dimeBasecombcriterionDto.setCombId(dimeBasecombcriterion.getCombId());
        dimeBasecombcriterionDto.setCombName(dimeBasecombcriterion.getCombName());
        dimeBasecombcriterionDto.setRequired(dimeBasecombcriterion.getRequired());
        dimeBasecombcriterionDto.setTarget(dimeBasecombcriterion.getTarget());
        dimeBasecombcriterionDto.setSn(dimeBasecombcriterion.getSn());
        dimeBasecombcriterionDto.setIsValid(dimeBasecombcriterion.getIsValid());
        dimeBasecombcriterionDto.setSearchValue(dimeBasecombcriterion.getSearchValue());
        dimeBasecombcriterionDto.setCreateBy(dimeBasecombcriterion.getCreateBy());
        dimeBasecombcriterionDto.setCreateTime(dimeBasecombcriterion.getCreateTime());
        dimeBasecombcriterionDto.setUpdateBy(dimeBasecombcriterion.getUpdateBy());
        dimeBasecombcriterionDto.setUpdateTime(dimeBasecombcriterion.getUpdateTime());
        dimeBasecombcriterionDto.setRemark(dimeBasecombcriterion.getRemark());
        dimeBasecombcriterionDto.setParams(dimeBasecombcriterion.getParams());
        return dimeBasecombcriterionDto;

    }



    @Override
    @Transactional(rollbackFor = Exception.class)
    public int SaveMaincheck(MainchecksubmitDto mainchecksubmitDto) {

        int rows = 0;


        DimePhysicalregister dimephysicalregister = dimePhysicalregisterMapper.selectDimePhysicalregisterById(mainchecksubmitDto.getId());

        List<PackageItem> packageItems = new ArrayList<>();

        //获取基础结论
        R<DimeBasemaincheckconclusion>dimeBasemaincheckconclusionR=remoteBaseMainclusionService.getMaincheckconclusionByid(mainchecksubmitDto.getDimeMaincheckconclusion().getConclusionId());
        if(dimeBasemaincheckconclusionR.getCode()!=R.FAIL)
        {
            DimeBasemaincheckconclusion dimeBasemaincheckconclusion=dimeBasemaincheckconclusionR.getData();
            mainchecksubmitDto.dimeMaincheckconclusion.setPassTag(dimeBasemaincheckconclusion.getPassTag());
            mainchecksubmitDto.dimeMaincheckconclusion.setRecheckTag(dimeBasemaincheckconclusion.getRecheckTag());
            mainchecksubmitDto.dimeMaincheckconclusion.setOccupationalTag(dimeBasemaincheckconclusion.getOccupationalTag());
            mainchecksubmitDto.dimeMaincheckconclusion.setContraindicationTag(dimeBasemaincheckconclusion.getContraindicationTag());
            mainchecksubmitDto.dimeMaincheckconclusion.setOtherTag(dimeBasemaincheckconclusion.getOtherTag());

        }



        //保存主检结论
        if (mainchecksubmitDto.getDimeMaincheckconclusion() != null) {
            rows += saveMaincheckconclusion(mainchecksubmitDto.getDimeMaincheckconclusion(), dimephysicalregister);
        }

        //健康建议
        if (mainchecksubmitDto.getDimeMaincheckhealthadviceList() != null && mainchecksubmitDto.getDimeMaincheckhealthadviceList().size() > 0) {
            rows += saveMaincheckhealthadvice(mainchecksubmitDto.getDimeMaincheckhealthadviceList(), dimephysicalregister);

        }


        //危害对应的关系表
        if (mainchecksubmitDto.maincheckHarmDtoList != null && mainchecksubmitDto.maincheckHarmDtoList.size() > 0) {
            rows += saveMaincheckharms(mainchecksubmitDto.maincheckHarmDtoList, dimephysicalregister, mainchecksubmitDto.dimeMaincheckconclusion, packageItems);
        }

        //保存其他异常

        if(Objects.equals(mainchecksubmitDto.dimeMaincheckconclusion.getOtherTag(), "1") ) {

            if (mainchecksubmitDto.dimeMaincheckotherdisease != null && mainchecksubmitDto.abnormalDimePhysicalitems != null && mainchecksubmitDto.abnormalDimePhysicalitems.size() > 0) {
                rows += saveMaincheckotherdiesase(mainchecksubmitDto.dimeMaincheckotherdisease, dimephysicalregister);
                rows += saveMaincheckotherdiesasecomb(mainchecksubmitDto.dimeMaincheckotherdisease, mainchecksubmitDto.abnormalDimePhysicalitems, dimephysicalregister);

            } else {
                throw new ServiceException(dimephysicalregister.getName() + "未选择异常项目！");
            }

        }


        //推送扁鹊复查项目
        if(!PushBQReckeckCombs(packageItems, "push", dimephysicalregister))
        {
            System.out.print(dimephysicalregister.getName()+">>>复查项目推送扁鹊失败！----------------------");
          //  throw new ServiceException(dimephysicalregister.getName() + "复查项目推送扁鹊失败！");
        }

        //更新主表信息
        dimephysicalregister.setCompletStatus("2");
        dimephysicalregister.setMainCheckDate(DateUtils.getNowDate());
        dimephysicalregister.setMainCheckDoctor(SecurityUtils.getLoginUser().getUsername());
        dimephysicalregister.setCheckResult(mainchecksubmitDto.getDimeMaincheckconclusion().getAbnormalresult());
        dimephysicalregister.setMainConclusion(mainchecksubmitDto.getDimeMaincheckconclusion().getConclusion());
        dimephysicalregister.setHealthSuggestion(mainchecksubmitDto.getDimeMaincheckconclusion().getConclusionadvice());
        dimePhysicalregisterMapper.updateDimePhysicalregister(dimephysicalregister);

        return rows;

    }

    private int saveMaincheckotherdiesasecomb(DimeMaincheckotherdisease dimeMaincheckotherdisease,List<DimePhysicalitems> dimePhysicalitemsList,DimePhysicalregister dimePhysicalregister) {
        int rows = 0;
        for (DimePhysicalitems dimePhysicalitems:dimePhysicalitemsList) {
            DimeMaincheckotherdiseaecomb dimeMaincheckotherdiseaecomb=new DimeMaincheckotherdiseaecomb();
            dimeMaincheckotherdiseaecomb.setId(IdUtils.fastUUID());
            dimeMaincheckotherdiseaecomb.setCreateTime(DateUtils.getNowDate());
            dimeMaincheckotherdiseaecomb.setPhysicalRegisterId(dimePhysicalregister.getId());
            dimeMaincheckotherdiseaecomb.setCreateBy(SecurityUtils.getLoginUser().getUsername());
            dimeMaincheckotherdiseaecomb.setCombId(dimePhysicalitems.getBaseCombId());
            dimeMaincheckotherdiseaecomb.setCombName(dimePhysicalitems.getCombName());
            dimeMaincheckotherdiseaecomb.setCombCode(dimePhysicalitems.getCombCode());
            dimeMaincheckotherdiseaecomb.setOtherdiseaeId(dimeMaincheckotherdisease.getId());
            dimeMaincheckotherdiseaecomb.setOtherdiseaeName(dimeMaincheckotherdisease.getOtherdiseaName());
            rows+=  dimeMaincheckotherdiseaecombMapper.insertDimeMaincheckotherdiseaecomb(dimeMaincheckotherdiseaecomb);
        }

        return rows;

    }

    private int saveMaincheckotherdiesase(DimeMaincheckotherdisease dimeMaincheckotherdisease,DimePhysicalregister dimePhysicalregister) {
        int rows = 0;
        dimeMaincheckotherdisease.setId(IdUtils.fastUUID());
        dimeMaincheckotherdisease.setCreateTime(DateUtils.getNowDate());
        dimeMaincheckotherdisease.setPhysicalRegisterId(dimePhysicalregister.getId());
        dimeMaincheckotherdisease.setCreateBy(SecurityUtils.getLoginUser().getUsername());

        rows += dimeMaincheckotherdiseaseMapper.insertDimeMaincheckotherdisease(dimeMaincheckotherdisease);

        return rows;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int RevokeMaincheck(DimePhysicalregister _dimePhysicalregister) {

        int rows = 0;

        //撤销主表
        DimePhysicalregister dimephysicalregister = dimePhysicalregisterMapper.selectDimePhysicalregisterById(_dimePhysicalregister.getId());
        dimephysicalregister.setCompletStatus("1");
        dimephysicalregister.setMainCheckDate(null);
        dimephysicalregister.setMainCheckDoctor("");
        dimephysicalregister.setCheckResult("");
        dimephysicalregister.setMainConclusion("");
        dimephysicalregister.setHealthSuggestion("");
        dimephysicalregister.setMainCheckRevoke(_dimePhysicalregister.getMainCheckRevoke());
        dimephysicalregister.setMainCheckRevokeMark(_dimePhysicalregister.getMainCheckRevokeMark());

        rows += dimePhysicalregisterMapper.updateDimePhysicalregister(dimephysicalregister);


        //危害异常项目
        rows += dimeMaincheckharmabnormalcombMapper.deleteDimeMaincheckharmabnormalcombByPhysicalId(dimephysicalregister.getId());
        //危害禁忌症
        rows += dimeMaincheckharmcontraindicationMapper.deleteDimeMaincheckharmcontraindicationByPhysicalId(dimephysicalregister.getId());
        //危害职业病
        rows += dimeMaincheckharmoccupationaldiseaeMapper.deleteDimeMaincheckharmoccupationaldiseaeByPhysicalId(dimephysicalregister.getId());
        //危害其他疾病
        rows += dimeMaincheckharmotherdiseaseMapper.deleteDimeMaincheckharmotherdiseaseByPhysicalId(dimephysicalregister.getId());
        //危害复查项目
        rows += dimeMaincheckharmrecheckcombMapper.deleteDimeMaincheckharmrecheckcombByPhysicalId(dimephysicalregister.getId());

        //主检结论
        rows += dimeMaincheckconclusionMapper.deleteDimeMaincheckconclusionByPhysicalId(dimephysicalregister.getId());
        //健康建议
        rows += dimeMaincheckhealthadviceMapper.deleteDimeMaincheckhealthadviceByPhysicalId(dimephysicalregister.getId());

        //危害结论
        rows += dimeMaincheckharmconclusionMapper.deleteDimeMaincheckharmconclusionByPhysicalId(dimephysicalregister.getId());
        //危害结论建议
        rows += dimeMaincheckharmadviceMapper.deleteDimeMaincheckharmadviceByPhysicalId(dimephysicalregister.getId());

        //撤销复查项目
        DimeMaincheckharmrecheckcomb maincheckharmrecheckcomb = new DimeMaincheckharmrecheckcomb();
        maincheckharmrecheckcomb.setPhysicalRegisterId(dimephysicalregister.getId());

        //其他异常
        rows+=dimeMaincheckotherdiseaseMapper.deleteDimeMaincheckotherdiseaseByPhysicalId(dimephysicalregister.getId());
        //其他异常项目
        rows+=dimeMaincheckotherdiseaecombMapper.deleteDimeMaincheckotherdiseaecombByPhysicalId(dimephysicalregister.getId());


        List<DimeMaincheckharmrecheckcomb> dimeMaincheckharmrecheckcombs = dimeMaincheckharmrecheckcombMapper.selectDimeMaincheckharmrecheckcombList(maincheckharmrecheckcomb);
        List<PackageItem> recheckitem = new ArrayList<>();
        for (DimeMaincheckharmrecheckcomb dimeMaincheckharmrecheckcomb : dimeMaincheckharmrecheckcombs) {
            PackageItem item = new PackageItem();
            item.setItemCode(dimeMaincheckharmrecheckcomb.getCombCode());
            item.setItemName(dimeMaincheckharmrecheckcomb.getCombName());
            recheckitem.add(item);

        }

        if (recheckitem != null && recheckitem.size() > 0) {

            if (!PushBQReckeckCombs(recheckitem, "cancel", dimephysicalregister)) {
                throw new ServiceException(dimephysicalregister.getName() + "撤销复查项目推送扁鹊失败！");
            }
        }

        return rows;
    }

    @Override
    public MainchecksubmitDto getSubmitMaincheckPhysical(DimePhysicalregister dimePhysicalregister) {

        MainchecksubmitDto mainchecksubmitDto = submitMappingPhysical(dimePhysicalregister);


        DimeMaincheckhealthadvice searchhealthadvice = new DimeMaincheckhealthadvice();
        searchhealthadvice.setPhysicalRegisterId(dimePhysicalregister.getId());

        //已提交健康建议
        List<DimeMaincheckhealthadvice> dimeMaincheckhealthadviceList = dimeMaincheckhealthadviceMapper.selectDimeMaincheckhealthadviceList(searchhealthadvice);
        mainchecksubmitDto.setDimeMaincheckhealthadviceList(dimeMaincheckhealthadviceList);

        // 主检结论

        DimeMaincheckconclusion dimeMaincheckconclusion = dimeMaincheckconclusionMapper.selectDimeMaincheckconclusionByPhysicalId(dimePhysicalregister.getId());
        mainchecksubmitDto.setDimeMaincheckconclusion(dimeMaincheckconclusion);


        //其他疾病异常
        DimeMaincheckotherdisease dimeMaincheckotherdisease = dimeMaincheckotherdiseaseMapper.selectDimeMaincheckotherdiseaseByPhysicalId(dimePhysicalregister.getId());
        mainchecksubmitDto.setDimeMaincheckotherdisease(dimeMaincheckotherdisease);

        //其他疾病异常 项目

        List<DimeMaincheckotherdiseaecomb> dimeMaincheckotherdiseaecombList = dimeMaincheckotherdiseaecombMapper.selectDimeMaincheckotherdiseaecombByPhysicalId(dimePhysicalregister.getId());
        if (dimeMaincheckotherdiseaecombList != null) {
            List<DimePhysicalitems> AbnormalDimePhysicalitems = new ArrayList<>();

            for (DimeMaincheckotherdiseaecomb dimeMaincheckotherdiseaecomb : dimeMaincheckotherdiseaecombList) {
                DimePhysicalitems dimePhysicalitems = new DimePhysicalitems();

                dimePhysicalitems.setPhysicalRegisterId(dimePhysicalregister.getId());
                dimePhysicalitems.setBaseCombId(dimeMaincheckotherdiseaecomb.getCombId());
                dimePhysicalitems.setCombName(dimeMaincheckotherdiseaecomb.getCombName());
                dimePhysicalitems.setCombCode(dimeMaincheckotherdiseaecomb.getCombCode());
                AbnormalDimePhysicalitems.add(dimePhysicalitems);
            }
            mainchecksubmitDto.setAbnormalDimePhysicalitems(AbnormalDimePhysicalitems);
        }


        // 危害相关

        mainchecksubmitDto.maincheckHarmDtoList = new ArrayList<>();


        DimeMaincheckharmconclusion searchharmconclusion = new DimeMaincheckharmconclusion();
        searchharmconclusion.setPhysicalRegisterId(dimePhysicalregister.getId());

        List<DimeMaincheckharmconclusion> dimeMaincheckharmconclusionList = dimeMaincheckharmconclusionMapper.selectDimeMaincheckharmconclusionList(searchharmconclusion);


        List<MaincheckHarmsubmitDto> maincheckHarmsubmitDtoList = new ArrayList<>();
        for (DimeMaincheckharmconclusion dimeMaincheckharmconclusion : dimeMaincheckharmconclusionList) {

            MaincheckHarmsubmitDto harmsubmitDto = new MaincheckHarmsubmitDto();


            harmsubmitDto.setBaseHarmId(dimeMaincheckharmconclusion.getHarmId());
            harmsubmitDto.setBaseHarmName(dimeMaincheckharmconclusion.getHarmName());
            harmsubmitDto.setConclusionId(dimeMaincheckharmconclusion.getConclusionId());

            //危害结论
            harmsubmitDto.setDimeMaincheckharmconclusion(dimeMaincheckharmconclusion);
            //危害建议


            //危害结论建议
            DimeMaincheckharmadvice searchharmadvice = new DimeMaincheckharmadvice();
            searchharmadvice.setPhysicalRegisterId(dimePhysicalregister.getId());
            searchharmadvice.setHarmId(dimeMaincheckharmconclusion.getHarmId());

            DimeMaincheckharmadvice dimeMaincheckharmadvice = dimeMaincheckharmadviceMapper.selectDimeMaincheckharmadviceList(searchharmadvice).stream().findFirst().orElse(null);
            harmsubmitDto.setDimeMaincheckharmadvice(dimeMaincheckharmadvice);


            //危害对应 基础结论的 建议list

            DimeBasemaincheckconclusionadvice basemaincheckconclusionadvice = new DimeBasemaincheckconclusionadvice();
            basemaincheckconclusionadvice.setMainconclusionId(dimeMaincheckharmconclusion.getConclusionId());


            R<List<DimeBasemaincheckconclusionadvice>> listR = remoteBaseMaincheckHarmAdvice.getconclusionadviceList(basemaincheckconclusionadvice, SecurityConstants.INNER);

            if (listR.getCode() != R.FAIL) {
                List<DimeBasemaincheckconclusionadvice> dimeBasemaincheckconclusionadviceList = listR.getData();
                if (dimeBasemaincheckconclusionadviceList != null) {
                    harmsubmitDto.setConclusionadviceList(dimeBasemaincheckconclusionadviceList);
                }
            }


            //危害异常项目
            DimeMaincheckharmabnormalcomb searchabnormalcomb = new DimeMaincheckharmabnormalcomb();
            searchabnormalcomb.setPhysicalRegisterId(dimePhysicalregister.getId());
            searchabnormalcomb.setHarmId(dimeMaincheckharmconclusion.getHarmId());

            List<DimeMaincheckharmabnormalcomb> dimeMaincheckharmabnormalcombList = dimeMaincheckharmabnormalcombMapper.selectDimeMaincheckharmabnormalcombList(searchabnormalcomb);


            harmsubmitDto.setDimeMaincheckharmabnormalcombList(dimeMaincheckharmabnormalcombList);

            //危害 疑似职业病

            DimeMaincheckharmoccupationaldiseae searchharmsuspect = new DimeMaincheckharmoccupationaldiseae();
            searchharmsuspect.setPhysicalRegisterId(dimePhysicalregister.getId());
            searchharmsuspect.setHarmId(dimeMaincheckharmconclusion.getHarmId());

            List<DimeMaincheckharmoccupationaldiseae> dimeMaincheckharmoccupationaldiseaeList = dimeMaincheckharmoccupationaldiseaeMapper.selectDimeMaincheckharmoccupationaldiseaeList(searchharmsuspect);
            harmsubmitDto.setDimeMaincheckharmoccupationaldiseaeList(dimeMaincheckharmoccupationaldiseaeList);

            //危害 禁忌症

            DimeMaincheckharmcontraindication searchharcontraindication = new DimeMaincheckharmcontraindication();
            searchharcontraindication.setPhysicalRegisterId(dimePhysicalregister.getId());
            searchharcontraindication.setHarmId(dimeMaincheckharmconclusion.getHarmId());

            List<DimeMaincheckharmcontraindication> dimeMaincheckharmcontraindicationList = dimeMaincheckharmcontraindicationMapper.selectDimeMaincheckharmcontraindicationList(searchharcontraindication);
            harmsubmitDto.setDimeMaincheckharmcontraindicationList(dimeMaincheckharmcontraindicationList);

            //基础职业病和禁忌症list
            DimePhysicalharms searchharms = new DimePhysicalharms();
            searchharms.setPhysicalRegisterId(dimePhysicalregister.getId());
            searchharms.setBaseHarmId(dimeMaincheckharmconclusion.getHarmId());


            DimePhysicalharms dimePhysicalharm = dimePhysicalharmsMapper.selectDimePhysicalharmsList(searchharms).stream().findFirst().orElse(null);

            if (dimePhysicalharm != null) {
                MaincheckHarmViewDto maincheckHarmViewDto = getMaincheckHarmViewDto(dimePhysicalharm, null, dimePhysicalregister);
                harmsubmitDto.setDimeBasecontraindicationcriterionList(maincheckHarmViewDto.getDimeBasecontraindicationcriterionList());
                harmsubmitDto.setDimeBaseoccupationaldiseasecriterionList(maincheckHarmViewDto.getDimeBaseoccupationaldiseasecriterionList());
            }


            //危害其他异常
            DimeMaincheckharmotherdisease searchotherdisease = new DimeMaincheckharmotherdisease();
            searchotherdisease.setPhysicalRegisterId(dimePhysicalregister.getId());
            searchotherdisease.setHarmId(dimeMaincheckharmconclusion.getHarmId());

            List<DimeMaincheckharmotherdisease> dimeMaincheckharmotherdiseaseList = dimeMaincheckharmotherdiseaseMapper.selectDimeMaincheckharmotherdiseaseList(searchotherdisease);
            harmsubmitDto.setDimeMaincheckharmotherdiseaseList(dimeMaincheckharmotherdiseaseList);

            if (dimeMaincheckharmotherdiseaseList != null && dimeMaincheckharmotherdiseaseList.size() > 0) {
                harmsubmitDto.otherdiseaName = StringUtils.join(dimeMaincheckharmotherdiseaseList.stream().map(DimeMaincheckharmotherdisease::getOtherdiseaName), ",");
            }


            //危害对应复查项目

            List<DimeMaincheckharmrecheckcomb> dimeMaincheckharmrecheckcombList = dimeMaincheckharmrecheckcombMapper.selectDimeMaincheckharmrecheckcombByPhysicalId(dimePhysicalregister.getId());
            if (dimeMaincheckharmrecheckcombList != null) {

                List<DimeMaincheckharmrecheckcomb> harmrecheckcombList = dimeMaincheckharmrecheckcombList.stream().filter(x -> Objects.equals(x.getHarmId(), dimeMaincheckharmconclusion.getHarmId())).collect(Collectors.toList());
                harmsubmitDto.setDimeMaincheckharmrecheckcombList(harmrecheckcombList);
            }


            maincheckHarmsubmitDtoList.add(harmsubmitDto);

        }

        mainchecksubmitDto.setMaincheckHarmDtoList(maincheckHarmsubmitDtoList);

        return mainchecksubmitDto;
    }

    @Override
    public List<DimeHistoryPhysicalDto> getHistoryPhysical(DimePhysicalregister dimePhysicalregister) {


        List<DimeHistoryPhysicalDto> dimeHistoryPhysicalDtos=new ArrayList<>();

        SearchPhysicalregister searchPhysicalregister=new SearchPhysicalregister();
        searchPhysicalregister.setIdNo(dimePhysicalregister.getIdNo());
        searchPhysicalregister.setCheckCode(dimePhysicalregister.getCheckCode());

        List<DimePhysicalregister>dimePhysicalregisters=dimePhysicalregisterMapper.selectDimeHistoryPhysicalregisterList(searchPhysicalregister);
        if(dimePhysicalregisters!=null&&dimePhysicalregisters.size()>0)
        {
            for (DimePhysicalregister Physicalregister:dimePhysicalregisters) {

                DimeHistoryPhysicalDto dimeHistoryPhysicalDto=MappingHistoryPhysical(Physicalregister);

                //项目 小结 流程
                RegisterViewDto registerViewDto= dimePhysicalregisterService.getPhysicalInfo(Physicalregister);
                dimeHistoryPhysicalDto.setRegisterflowViewDtoList(registerViewDto.getRegisterflowViewDtoList());

                //小组图片
                List<PhysicalGroupPicDto> physicalGroupPicDtoList = getPhysicalGroupPic(dimePhysicalregister);
                dimeHistoryPhysicalDto.setPhysicalGroupPicDtoList(physicalGroupPicDtoList);

                //主检
                DimeMaincheckconclusion dimeMaincheckconclusion=dimeMaincheckconclusionMapper.selectDimeMaincheckconclusionByPhysicalId(Physicalregister.getId());
                dimeHistoryPhysicalDto.setDimeMaincheckconclusion(dimeMaincheckconclusion);

                dimeHistoryPhysicalDto.mainCheckHistoryDto=new MainCheckHistoryDto();

                dimeHistoryPhysicalDto.mainCheckHistoryDto.setAbnormalResults(Physicalregister.getCheckResult());

                List<DimeMaincheckharmconclusion> dimeMaincheckharmconclusionList = dimeMaincheckharmconclusionMapper.selectDimeMaincheckharmconclusionByPhysicalId(Physicalregister.getId());
                dimeHistoryPhysicalDto.mainCheckHistoryDto.setDimeMaincheckharmconclusionList(dimeMaincheckharmconclusionList);


                //问诊
                if(Objects.equals(Physicalregister.getNeedAsk(), "1"))
                {
                    InquiryDto inquiryDtos = dimeInquiryService.inquiryDto(Physicalregister.getCheckCode());
                    if(inquiryDtos!=null)
                    {
                        dimeHistoryPhysicalDto.inquiryHistoryDto=new InquiryHistoryDto();

                        dimeHistoryPhysicalDto.inquiryHistoryDto.setInquirydisease(inquiryDtos.getInquirydisease());
                        dimeHistoryPhysicalDto.inquiryHistoryDto.setInquiryoccupations(inquiryDtos.getInquiryoccupations());
                        dimeHistoryPhysicalDto.inquiryHistoryDto.setInquiryitems(inquiryDtos.getInquiryitems());
                        dimeHistoryPhysicalDto.inquiryHistoryDto.setInquirychronicoccupationalhistory(inquiryDtos.getInquirychronicoccupationalhistory());
                        dimeHistoryPhysicalDto.inquiryHistoryDto.setInquirycurrentdiseases(inquiryDtos.getInquirycurrentdiseases());
                        dimeHistoryPhysicalDto.inquiryHistoryDto.setInquiryradiationoccupations(inquiryDtos.getInquiryradiationoccupations());
                        dimeHistoryPhysicalDto.inquiryHistoryDto.setInquiryradiationother(inquiryDtos.getInquiryradiationother());
                        dimeHistoryPhysicalDto.inquiryHistoryDto.setInquiryradiationoccupations(inquiryDtos.getInquiryradiationoccupations());
                        dimeHistoryPhysicalDto.inquiryHistoryDto.setInquirysymptom(inquiryDtos.getInquirysymptom());
                        dimeHistoryPhysicalDto.inquiryHistoryDto.setInquirysign(inquiryDtos.getInquirysign());
                    }
                }
                dimeHistoryPhysicalDtos.add(dimeHistoryPhysicalDto);
            }
        }


        return dimeHistoryPhysicalDtos;
    }

    @Override
    public int MainCheckExamine(SearchPhysicalregister searchPhysicalregister) {

        int rows=0;

        DimePhysicalregister dimePhysicalregister=dimePhysicalregisterMapper.selectDimePhysicalregisterById(searchPhysicalregister.getId());

        dimePhysicalregister.setCompletStatus("3");
        rows+=dimePhysicalregisterMapper.updateDimePhysicalregister(dimePhysicalregister);

        return rows;
    }

    @Override
    public int MainCheckUnExamine(SearchPhysicalregister searchPhysicalregister) {
        int rows=0;
        DimePhysicalregister dimePhysicalregister=dimePhysicalregisterMapper.selectDimePhysicalregisterById(searchPhysicalregister.getId());

        dimePhysicalregister.setCompletStatus("2");
        rows+=dimePhysicalregisterMapper.updateDimePhysicalregister(dimePhysicalregister);
        return rows;
    }

    private DimeHistoryPhysicalDto MappingHistoryPhysical(DimePhysicalregister physicalregister) {
        DimeHistoryPhysicalDto dimeHistoryPhysicalDto = new DimeHistoryPhysicalDto();
        dimeHistoryPhysicalDto.setHeadUrl(physicalregister.getHeadUrl());
        dimeHistoryPhysicalDto.setHeadpicture(physicalregister.getHeadpicture());
        dimeHistoryPhysicalDto.setNation(physicalregister.getNation());
        dimeHistoryPhysicalDto.setId(physicalregister.getId());
        dimeHistoryPhysicalDto.setCheckCode(physicalregister.getCheckCode());
        dimeHistoryPhysicalDto.setIdNo(physicalregister.getIdNo());
        dimeHistoryPhysicalDto.setName(physicalregister.getName());
        dimeHistoryPhysicalDto.setPersonType(physicalregister.getPersonType());
        dimeHistoryPhysicalDto.setCheckType(physicalregister.getCheckType());
        dimeHistoryPhysicalDto.setIdNoType(physicalregister.getIdNoType());
        dimeHistoryPhysicalDto.setSex(physicalregister.getSex());
        dimeHistoryPhysicalDto.setAge(physicalregister.getAge());
        dimeHistoryPhysicalDto.setMarriage(physicalregister.getMarriage());
        dimeHistoryPhysicalDto.setPhone(physicalregister.getPhone());
        dimeHistoryPhysicalDto.setAddress(physicalregister.getAddress());
        dimeHistoryPhysicalDto.setBirthday(physicalregister.getBirthday());
        dimeHistoryPhysicalDto.setBasePackageId(physicalregister.getBasePackageId());
        dimeHistoryPhysicalDto.setPackageName(physicalregister.getPackageName());
        dimeHistoryPhysicalDto.setBaseCompanyId(physicalregister.getBaseCompanyId());
        dimeHistoryPhysicalDto.setCompanyName(physicalregister.getCompanyName());
        dimeHistoryPhysicalDto.setRegisterDate(physicalregister.getRegisterDate());
        dimeHistoryPhysicalDto.setCheckDate(physicalregister.getCheckDate());
        dimeHistoryPhysicalDto.setChannel(physicalregister.getChannel());
        dimeHistoryPhysicalDto.setReportPrintDate(physicalregister.getReportPrintDate());
        dimeHistoryPhysicalDto.setMainCheckDate(physicalregister.getMainCheckDate());
        dimeHistoryPhysicalDto.setReportPrinter(physicalregister.getReportPrinter());
        dimeHistoryPhysicalDto.setJobType(physicalregister.getJobType());
        dimeHistoryPhysicalDto.setEmpNo(physicalregister.getEmpNo());
        dimeHistoryPhysicalDto.setDept(physicalregister.getDept());
        dimeHistoryPhysicalDto.setWorkStatus(physicalregister.getWorkStatus());
        dimeHistoryPhysicalDto.setCompletStatus(physicalregister.getCompletStatus());
        dimeHistoryPhysicalDto.setHarmfacter(physicalregister.getHarmfacter());
        dimeHistoryPhysicalDto.setPassword(physicalregister.getPassword());
        dimeHistoryPhysicalDto.setWorkYears(physicalregister.getWorkYears());
        dimeHistoryPhysicalDto.setWorkMonths(physicalregister.getWorkMonths());
        dimeHistoryPhysicalDto.setTouchYears(physicalregister.getTouchYears());
        dimeHistoryPhysicalDto.setTouchMonths(physicalregister.getTouchMonths());
        dimeHistoryPhysicalDto.setNeedAsk(physicalregister.getNeedAsk());
        dimeHistoryPhysicalDto.setReCheckTag(physicalregister.getReCheckTag());
        dimeHistoryPhysicalDto.setLastCheckCode(physicalregister.getLastCheckCode());
        dimeHistoryPhysicalDto.setCombLack(physicalregister.getCombLack());
        dimeHistoryPhysicalDto.setSocialCode(physicalregister.getSocialCode());
        dimeHistoryPhysicalDto.setServiceUid(physicalregister.getServiceUid());
        dimeHistoryPhysicalDto.setSoaOrderNo(physicalregister.getSoaOrderNo());
        dimeHistoryPhysicalDto.setPlatformTag(physicalregister.getPlatformTag());
        dimeHistoryPhysicalDto.setCheckResult(physicalregister.getCheckResult());
        dimeHistoryPhysicalDto.setMainConclusion(physicalregister.getMainConclusion());
        dimeHistoryPhysicalDto.setHealthSuggestion(physicalregister.getHealthSuggestion());
        dimeHistoryPhysicalDto.setMainCheckDoctor(physicalregister.getMainCheckDoctor());
        dimeHistoryPhysicalDto.setTakeReportType(physicalregister.getTakeReportType());
        dimeHistoryPhysicalDto.setIsValid(physicalregister.getIsValid());
        dimeHistoryPhysicalDto.setShopCode(physicalregister.getShopCode());
        dimeHistoryPhysicalDto.setTouchHarms(physicalregister.getTouchHarms());
        dimeHistoryPhysicalDto.setNationality(physicalregister.getNationality());
        dimeHistoryPhysicalDto.setRegistertype(physicalregister.getRegistertype());
        dimeHistoryPhysicalDto.setSearchValue(physicalregister.getSearchValue());
        dimeHistoryPhysicalDto.setCreateBy(physicalregister.getCreateBy());
        dimeHistoryPhysicalDto.setCreateTime(physicalregister.getCreateTime());
        dimeHistoryPhysicalDto.setUpdateBy(physicalregister.getUpdateBy());
        dimeHistoryPhysicalDto.setUpdateTime(physicalregister.getUpdateTime());
        dimeHistoryPhysicalDto.setRemark(physicalregister.getRemark());
        dimeHistoryPhysicalDto.setParams(physicalregister.getParams());
        return dimeHistoryPhysicalDto;

    }


    private int saveMaincheckharms(List<MaincheckHarmsubmitDto> maincheckHarmsubmitDtos, DimePhysicalregister dimephysicalregister, DimeMaincheckconclusion dimeMaincheckconclusion, List<PackageItem> packageItemList) {
        int rows = 0;

        for (MaincheckHarmsubmitDto maincheckHarmsubmitDto : maincheckHarmsubmitDtos) {

            //危害对应的 结论建议 异常疾病
            if (maincheckHarmsubmitDto.getDimeMaincheckharmconclusion() != null) {

                DimeMaincheckharmconclusion dimeMaincheckharmconclusion = maincheckHarmsubmitDto.getDimeMaincheckharmconclusion();

                dimeMaincheckharmconclusion.setId(IdUtils.fastUUID());
                dimeMaincheckharmconclusion.setCreateTime(DateUtils.getNowDate());
                dimeMaincheckharmconclusion.setPhysicalRegisterId(dimephysicalregister.getId());
                dimeMaincheckharmconclusion.setCreateBy(SecurityUtils.getLoginUser().getUsername());

                dimeMaincheckharmconclusionMapper.insertDimeMaincheckharmconclusion(dimeMaincheckharmconclusion);

            } else {
                throw new ServiceException(maincheckHarmsubmitDto.getBaseHarmName() + "危害未下结论！");
            }

            //危害对应的 异常项目
            if (maincheckHarmsubmitDto.getDimeMaincheckharmabnormalcombList() != null && maincheckHarmsubmitDto.getDimeMaincheckharmabnormalcombList().size() > 0) {

                for (DimeMaincheckharmabnormalcomb dimeMaincheckharmabnormalcomb : maincheckHarmsubmitDto.getDimeMaincheckharmabnormalcombList()) {

                    dimeMaincheckharmabnormalcomb.setId(IdUtils.fastUUID());
                    dimeMaincheckharmabnormalcomb.setCreateTime(DateUtils.getNowDate());
                    dimeMaincheckharmabnormalcomb.setPhysicalRegisterId(dimephysicalregister.getId());
                    dimeMaincheckharmabnormalcomb.setHarmId(maincheckHarmsubmitDto.getBaseHarmId());
                    dimeMaincheckharmabnormalcomb.setHarmName(maincheckHarmsubmitDto.getBaseHarmName());

                    dimeMaincheckharmabnormalcomb.setCreateBy(SecurityUtils.getLoginUser().getUsername());
                    rows += dimeMaincheckharmabnormalcombMapper.insertDimeMaincheckharmabnormalcomb(dimeMaincheckharmabnormalcomb);
                }
            }

            //危害对应的 建议
            if (maincheckHarmsubmitDto.getDimeMaincheckharmadvice() != null) {

                DimeMaincheckharmadvice dimeMaincheckharmadvice = maincheckHarmsubmitDto.getDimeMaincheckharmadvice();

                dimeMaincheckharmadvice.setId(IdUtils.fastUUID());
                dimeMaincheckharmadvice.setCreateTime(DateUtils.getNowDate());
                dimeMaincheckharmadvice.setPhysicalRegisterId(dimephysicalregister.getId());
                dimeMaincheckharmadvice.setCreateBy(SecurityUtils.getLoginUser().getUsername());
                rows += dimeMaincheckharmadviceMapper.insertDimeMaincheckharmadvice(dimeMaincheckharmadvice);

            }

            if (Objects.equals(maincheckHarmsubmitDto.getDimeMaincheckharmconclusion().getRecheckTag(), "1")) {
                //危害对应的复查项目
                if (maincheckHarmsubmitDto.getDimeMaincheckharmrecheckcombList() == null && maincheckHarmsubmitDto.getDimeMaincheckharmrecheckcombList().size() == 0) {
                    throw new ServiceException("复查结论,未获取到复查项目！");
                }
                for (DimeMaincheckharmrecheckcomb dimeMaincheckharmrecheckcomb : maincheckHarmsubmitDto.getDimeMaincheckharmrecheckcombList()) {

                    dimeMaincheckharmrecheckcomb.setId(IdUtils.fastUUID());
                    dimeMaincheckharmrecheckcomb.setCreateTime(DateUtils.getNowDate());
                    dimeMaincheckharmrecheckcomb.setPhysicalRegisterId(dimephysicalregister.getId());
                    dimeMaincheckharmrecheckcomb.setCreateBy(SecurityUtils.getLoginUser().getUsername());
                    dimeMaincheckharmrecheckcomb.setCombCode(dimeMaincheckharmrecheckcomb.getCombCode());
                    dimeMaincheckharmrecheckcomb.setIdno(dimephysicalregister.getIdNo());
                    dimeMaincheckharmrecheckcomb.setName(dimephysicalregister.getName());
                    dimeMaincheckharmrecheckcomb.setHarmId(maincheckHarmsubmitDto.getBaseHarmId());
                    dimeMaincheckharmrecheckcomb.setHarmName(maincheckHarmsubmitDto.getBaseHarmName());
                    dimeMaincheckharmrecheckcomb.setHarmconclusionId(maincheckHarmsubmitDto.getDimeMaincheckharmconclusion().getId());
                    dimeMaincheckharmrecheckcomb.setHarmconclusionName(maincheckHarmsubmitDto.getDimeMaincheckharmconclusion().getConclusion());
                  //  dimeMaincheckharmrecheckcomb.setAbnormaldesc(maincheckHarmsubmitDto.getAbnormaldesc());

                    rows += dimeMaincheckharmrecheckcombMapper.insertDimeMaincheckharmrecheckcomb(dimeMaincheckharmrecheckcomb);

                    PackageItem packageItem = new PackageItem();
                    packageItem.setItemCode(dimeMaincheckharmrecheckcomb.getCombCode());
                    packageItem.setItemName(dimeMaincheckharmrecheckcomb.getCombName());
                    packageItemList.add(packageItem);


                }
            }

            if (Objects.equals(maincheckHarmsubmitDto.getDimeMaincheckharmconclusion().getOccupationalTag(), "1")) {
                //危害对应的疑似职业病结论
                if (maincheckHarmsubmitDto.getDimeMaincheckharmoccupationaldiseaeList() == null && maincheckHarmsubmitDto.getDimeMaincheckharmoccupationaldiseaeList().size() == 0) {
                    throw new ServiceException("疑似职业病结论,未获取到职业病内容！");
                }
                for (DimeMaincheckharmoccupationaldiseae dimeMaincheckharmoccupationaldiseae : maincheckHarmsubmitDto.getDimeMaincheckharmoccupationaldiseaeList()) {

                    dimeMaincheckharmoccupationaldiseae.setId(IdUtils.fastUUID());
                    dimeMaincheckharmoccupationaldiseae.setCreateTime(DateUtils.getNowDate());
                    dimeMaincheckharmoccupationaldiseae.setPhysicalRegisterId(dimephysicalregister.getId());
                    dimeMaincheckharmoccupationaldiseae.setCreateBy(SecurityUtils.getLoginUser().getUsername());
                    dimeMaincheckharmoccupationaldiseae.setHarmconclusionId(maincheckHarmsubmitDto.getDimeMaincheckharmconclusion().getId());
                    dimeMaincheckharmoccupationaldiseae.setHarmconclusionName(maincheckHarmsubmitDto.getDimeMaincheckharmconclusion().getConclusion());
                  //  dimeMaincheckharmoccupationaldiseae.setAbnormaldesc(maincheckHarmsubmitDto.getAbnormaldesc());
                    rows += dimeMaincheckharmoccupationaldiseaeMapper.insertDimeMaincheckharmoccupationaldiseae(dimeMaincheckharmoccupationaldiseae);
                }

            }
            if (Objects.equals(maincheckHarmsubmitDto.getDimeMaincheckharmconclusion().getContraindicationTag(), "1")) {
                //危害对应的禁忌症
                if (maincheckHarmsubmitDto.getDimeMaincheckharmcontraindicationList() == null && maincheckHarmsubmitDto.getDimeMaincheckharmcontraindicationList().size() == 0) {
                    throw new ServiceException("禁忌症结论,未获取到禁忌症内容！");
                }
                for (DimeMaincheckharmcontraindication dimeMaincheckharmcontraindication : maincheckHarmsubmitDto.getDimeMaincheckharmcontraindicationList()) {
                    dimeMaincheckharmcontraindication.setId(IdUtils.fastUUID());
                    dimeMaincheckharmcontraindication.setCreateTime(DateUtils.getNowDate());
                    dimeMaincheckharmcontraindication.setPhysicalRegisterId(dimephysicalregister.getId());
                    dimeMaincheckharmcontraindication.setCreateBy(SecurityUtils.getLoginUser().getUsername());

                    dimeMaincheckharmcontraindication.setHarmconclusionId(maincheckHarmsubmitDto.getDimeMaincheckharmconclusion().getId());
                    dimeMaincheckharmcontraindication.setHarmconclusionName(maincheckHarmsubmitDto.getDimeMaincheckharmconclusion().getConclusion());
                    rows += dimeMaincheckharmcontraindicationMapper.insertDimeMaincheckharmcontraindication(dimeMaincheckharmcontraindication);
                }
            }


            if (Objects.equals(maincheckHarmsubmitDto.getDimeMaincheckharmconclusion().getOtherTag(), "1") && (maincheckHarmsubmitDto.otherdiseaName != null && !maincheckHarmsubmitDto.otherdiseaName.equals(""))) {

                DimeMaincheckharmotherdisease dimeMaincheckharmotherdisease = new DimeMaincheckharmotherdisease();
                dimeMaincheckharmotherdisease.setId(IdUtils.fastUUID());
                dimeMaincheckharmotherdisease.setCreateTime(DateUtils.getNowDate());
                dimeMaincheckharmotherdisease.setPhysicalRegisterId(dimephysicalregister.getId());
                dimeMaincheckharmotherdisease.setCreateBy(SecurityUtils.getLoginUser().getUsername());
                dimeMaincheckharmotherdisease.setHarmconclusionId(maincheckHarmsubmitDto.getDimeMaincheckharmconclusion().getId());
                dimeMaincheckharmotherdisease.setHarmconclusionName(maincheckHarmsubmitDto.getDimeMaincheckharmconclusion().getConclusion());
                dimeMaincheckharmotherdisease.setOtherdiseaName(maincheckHarmsubmitDto.otherdiseaName);
               // dimeMaincheckharmotherdisease.setAbnormaldesc(maincheckHarmsubmitDto.getAbnormaldesc());
                rows += dimeMaincheckharmotherdiseaseMapper.insertDimeMaincheckharmotherdisease(dimeMaincheckharmotherdisease);

            }


        }

        return rows;
    }


    private int saveMaincheckhealthadvice(List<DimeMaincheckhealthadvice> dimeMaincheckhealthadviceList, DimePhysicalregister dimephysicalregister) {
        int rows = 0;

        //循环保存 dimeMaincheckhealthadviceList
        for (DimeMaincheckhealthadvice dimeMaincheckhealthadvice : dimeMaincheckhealthadviceList) {

            dimeMaincheckhealthadvice.setId(IdUtils.fastUUID());
            dimeMaincheckhealthadvice.setCreateTime(DateUtils.getNowDate());
            dimeMaincheckhealthadvice.setPhysicalRegisterId(dimephysicalregister.getId());
            dimeMaincheckhealthadvice.setCreateBy(SecurityUtils.getLoginUser().getUsername());
            dimeMaincheckhealthadviceMapper.insertDimeMaincheckhealthadvice(dimeMaincheckhealthadvice);
        }
        return rows;
    }


    private int saveMaincheckconclusion(DimeMaincheckconclusion maincheckconclusion, DimePhysicalregister dimePhysicalregister) {
        int rows = 0;

        maincheckconclusion.setId(IdUtils.fastUUID());
        maincheckconclusion.setCreateTime(DateUtils.getNowDate());
        maincheckconclusion.setPhysicalRegisterId(dimePhysicalregister.getId());
        maincheckconclusion.setCreateBy(SecurityUtils.getLoginUser().getUsername());

        rows += dimeMaincheckconclusionMapper.insertDimeMaincheckconclusion(maincheckconclusion);


        return rows;
    }


    //扁鹊推送复查项目
    private boolean PushBQReckeckCombs(List<PackageItem> packageItems, String PostType, DimePhysicalregister dimePhysicalregister) {


        if (packageItems != null && packageItems.size() > 0) {
            DimeBasecompany basecompany = dimeBasecompanyMapper.selectDimeBasecompanyById(dimePhysicalregister.getBaseCompanyId());
            RecheckRequestBody requestBody = new RecheckRequestBody();
            String shopCode = SecurityUtils.getShopId();

            requestBody.setBranchCode(shopCode);
            UserShop userShop= SecurityUtils.getLoginUser().getShops().stream().findFirst().get();
            requestBody.setBranchName(userShop.getShopName());

            DimeBasepackage basepackage=dimeBasepackageMapper.selectDimeBasepackageById(dimePhysicalregister.getBasePackageId());
            if(basepackage!=null)
            {
                requestBody.setSoaOrderNo(basepackage.getOrderCode());
            }
            else
            {
                throw new ServiceException("获取SOA订单号失败！");
            }

            requestBody.setServiceUid(dimePhysicalregister.getServiceUid());
            requestBody.setTid(IdUtils.fastUUID());
            requestBody.setVid(dimePhysicalregister.getCheckCode());
            requestBody.setType(PostType);
            requestBody.setAge(String.valueOf(dimePhysicalregister.getAge()));
            if (basecompany != null) {
                requestBody.setCompanyCode(basecompany.getCompanyCode());
            }
            requestBody.setCertificateNumber(dimePhysicalregister.getIdNo());
            requestBody.setCertificateType(dimePhysicalregister.getCheckType());
            requestBody.setCompanyName(dimePhysicalregister.getCompanyName());
            requestBody.setCustomerName(dimePhysicalregister.getName());
            requestBody.setGender(dimePhysicalregister.getSex());
            requestBody.setCustomerPhone(dimePhysicalregister.getPhone());
            requestBody.setItems(packageItems);

            MNResponseBase mnResponseBase = remoteHealth100Service.postRecheck(requestBody);
            if (!Objects.equals(mnResponseBase.getCode(), "000000")) {
                System.out.print(mnResponseBase.getMsg());
                return false;

            }
//            else
//            {
//
//                throw new ServiceException(mnResponseBase.getMsg());
//            }
        }
        return true;
    }




}
